//
//  CZStatusCell.swift
//  CZWeibo
//
//  Created by 左凯旭 on 16/8/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class CZStatusCell: UITableViewCell {
    // 底部视图的顶部约束
    var bottomViewTopConstraint: Constraint?
    
    
    // 定义一个ViewModel 供外界赋值
    var statusUserViewModel:CZStatusUserViewModel?{
        didSet{
            
//            print(statusUserViewModel?.statusModel?.retweeted_status)
            
            //原创微博
            orginalView.statusUserViewModel = statusUserViewModel
         
            
            /*
             - 转发微博逻辑分析
             - 如果retweeted_status == nil  没有转发微博
             - 底部视图的top == 原创微博的bottom
             
             - 反之 有转发微博
             - 赋值
             - 底部视图的top == 转发微博的bottom
             */
            
            //先卸载之前的约束
            bottomViewTopConstraint?.uninstall()
            
            if statusUserViewModel?.statusModel?.retweeted_status == nil {
                //没有转发微博
                bootomView.snp_updateConstraints(closure: { (make) in
                    
                   bottomViewTopConstraint = make.top.equalTo(orginalView.snp_bottom).constraint
                })
                //隐藏转发微博
                retWeetView.hidden = true
            }else{
                //有转发微博
             
                retWeetView.statusUserViewModel = statusUserViewModel
                
                bootomView.snp_updateConstraints(closure: { (make) in
                    
                  bottomViewTopConstraint = make.top.equalTo(retWeetView.snp_bottom).constraint
                })
                //显示转发微博
                retWeetView.hidden = false
            }
            //赋值底部视图
            bootomView.statusUserModel = statusUserViewModel
            
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    func setupUI(){
    backgroundColor = RGB(237, green: 237, blue: 237)
        //添加控件
        contentView.addSubview(orginalView)
        contentView.addSubview(bootomView)
        contentView.addSubview(retWeetView)
        
        
        //设置约束
        orginalView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(8)
//            make.height.equalTo(50)
        }
        
        //转发微博
        retWeetView.snp_makeConstraints { (make) in
            make.top.equalTo(orginalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
//            make.height.equalTo(50)
        }
        
        
//        //底部视图
        bootomView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            //记录下距离顶部的约束
          bottomViewTopConstraint = make.top.equalTo(retWeetView.snp_bottom).constraint
            make.height.equalTo(35)
        }
        
        //自动计算行高
        contentView.snp_makeConstraints { (make) in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(bootomView)
        }
        
    }
    
    
    
    //懒加载控件
    private lazy var orginalView:CZStatusOriginalView = CZStatusOriginalView()
    
    //转发微博
    private lazy var retWeetView:CZRetWeetView = CZRetWeetView()
    
    //底部视图
    private lazy var bootomView:CZStatusBottomView = CZStatusBottomView()
    
}






